//prep the big psid file -- renaming, etc
clear all
do "$data/PSID/psid_all_import"
do "$data/PSID/psid_all_format"
gen uniqid = ER30001*1000 + ER30002 //generate of uniqid identifier variable
duplicates report uniqid //no duplicates!
drop ER30002 ER30000

//big renaming loop; use variable labels
ds uniqid, not
foreach var in `r(varlist)'{
	
	//fetch label
	local lab: variable label `var'
	
	//kill sequence/release/interview numbers
	if strpos("`lab'", "SEQUENCE") | strpos("`lab'", "RELEASE") | strpos("`lab'", "INTERVIEW") | strpos("`lab'", "LABOR"){
		drop `var'
		continue
	}
	
	//get year for individual files
	local num = word("`lab'", -1)
	
	//skip variables with labels that i don't like
	if length("`num'")!=2 | "`var'" == "V4373" {
		continue
	}	
	
	if `num'<20{ //2000s variable
		local year = 2000 + `num'
	}
	if `num'>20{ //1900s variable
		local year = 1900 + `num'
	}
	
	****rename according to label contents
	//age
	if strpos("`lab'", "AGE OF"){
		ren `var' age_`year'
	}
	
	//educ
	if strpos("`lab'", "EDUC") | strpos("`lab'", "GRADE") | strpos("`lab'", "SCHL") | strpos("`lab'", "GRAD F"){
		ren `var' educ_`year'
	}
	
	//weight
	if strpos("`lab'", "WEIGHT") | strpos("`lab'", " WT") {
		ren `var' weight_`year'
	}
	
	//relation to head
	if strpos("`lab'", "RELATION"){
		ren `var' relate_`year'
	}
}


//renaming of household variables
local years `"1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1999 2001 2003 2005 2007 2009 2011 2013 2015 2017"'
local counter1 =1
local counter2 = 1

ds age* relate* educ* weight* uniqid, not
foreach var in `r(varlist)'{

	//fetch label
	local lab: variable label `var'
	local y = word("`years'", `counter1') //select which year to use
	
	if strpos("`lab'", "STATE"){
		ren `var' state_`y'
	}
	
	if strpos("`lab'", "WAGE"){
		ren `var' wages_`y'
	}
	
	if strpos("`lab'", "HOURS") | strpos("`lab'", "HRS"){
		ren `var' hours_`y'
	}
	
	local counter2 = `counter2'+1 //update second counter
	if `counter2' == 4{ 
		local counter1 = `counter1'+1 //skip to next year
		local counter2 = 1 //refresh first counter
	}
}
ds age* relate* educ* weight* state* hours* wages* uniqid, not //everything has been renamed!!

//convert to long form
foreach y in `years'{
	preserve
	keep uniqid *_`y'
	gen year = `y'
	ren age age
	cap ren educ educ
	ren weight weight
	ren relate relate
	ren hours hours
	ren wages wages
	ren state state
	save "$temp/psid_long_`y'", replace
	restore
}

clear
foreach y in `years'{
	append using "$temp/psid_long_`y'"
}
sort uniqid year
order uniqid
compress
save "$temp/psid_long", replace
